<?php
namespace app\decorate\controller;
use app\AdminRest;
use app\ApiRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\decorate\model\IndexNews as news_model;
use app\decorate\model\AdminNewsCate;
use app\decorate\model\IndexCase;
use app\decorate\model\IndexCollect;


class IndexNews extends ApiRest {
    protected $model;
    protected $cate_model;
    protected $case_model;
    protected $collect_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model      = new news_model();
        $this->cate_model = new AdminNewsCate();
        $this->case_model = new IndexCase();
        $this->collect_model= new IndexCollect();

    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:文章列表
     */
    public function newsList(){
        $input = $this->_param;
        if(!empty($input['cate_id'])){
            $dis[] = ['cate_id','=',$input['cate_id']];
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data  = $this->model->newsList($dis,10);
        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-10-21 14:49
     * @return void
     * descption:文章首页
     */
    public function newsIndex(){
        $input = $this->_param;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data['news'] = $this->model->newsList($dis,10);

        $dis[] = ['status','=',1];
        $data['cate'] = $this->cate_model->cateSelect($dis);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:文章详情
     */
    public function newsInfo(){
        $input = $this->_param;
        $this->model->where(['id'=>$input['id']])->update(['iv'=>Db::raw('iv+1')]);
        $data['info'] = $this->model->newsInfo(['id'=>$input['id']]);
        if(empty($data['info'])){
            $this->collect_model->collectDel($this->getUserId(),$input['id'],3);
            return $this->error('not found news');
        }
        $collect = $this->collect_model->collectInfo(['user_id'=>$this->getUserId(),'type'=>3,'news_id'=>$data['info']['id'],'status'=>1]);
        $data['info']['is_collect'] = !empty($collect)?1:0;
        $data['case'] = $this->case_model->caseSelect(['recommend'=>1,'uniacid'=>$this->_uniacid]);

        return $this->success($data);
    }



    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status', '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }



}
